package com.cqhgcloud.monarch.organ.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cqhgcloud.monarch.organ.entity.SysUser;
import com.cqhgcloud.monarch.organ.service.SysUserService;

import lombok.AllArgsConstructor;

@Service
@AllArgsConstructor
public class LoginServiceImpl implements UserDetailsService {
	
	public SysUserService sysUserService;
	
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		SysUser user = sysUserService.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getUsername, username).eq(SysUser::getIsDeleted, 0));;
		List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
		getRoles(list);
		//把账号密码放置user中
		User auth_user = new User(username,user.getPassword(),list);
		return auth_user;
	}
	/*
	 * 获取所属角色
	 * @param list
	 */
	public void getRoles(List<GrantedAuthority> list) {
		for (String role : "USER,ADMIN,".split(",")) {
			list.add(new SimpleGrantedAuthority("ROLE_" + role));
		}
	}
}
